/**
 * 
 */
package com.speaktoitcookbook.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.speaktoitcookbook.bean.Feedback;
import com.speaktoitcookbook.dao.IFeedbackDao;
import com.speaktoitcookbook.util.DBUtil;
import com.speaktoitcookbook.util.TransformUtil;

/**
 * @author 赛艳志
 * 
 */
public class FeedbackDaoImpl implements IFeedbackDao {
    private Connection conn = null;

    public FeedbackDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doCreate(Feedback feedback) throws SQLException {
        boolean result = false;
        String sql = "insert into `tb_feedback`(`userID`, `fbState`, `fbDate`, `fbContent`) values(?, ?, ?, ?)";

        int item = 0;
        Object[] params = new Object[4];
        params[item++] = feedback.getUserID();
        params[item++] = feedback.getState();
        params[item++] = new Timestamp(feedback.getFeedbackDate().getTime());
        params[item++] = feedback.getContent();

        result = DBUtil.execute(conn, sql, params);
        return result;
    }

    @Override
    public boolean doDelete(long ID) throws SQLException {
        boolean result = false;
        String sql = "delete from `tb_feedbac`k where `fbID` = ?";

        result = DBUtil.execute(conn, sql, ID);
        return result;
    }

    @Override
    public boolean doDelete(Object... params) throws SQLException {
        boolean result = false;
        StringBuffer sql = new StringBuffer("delete from `tb_feedback` ");
        if (null != params && params.length > 0) {
            for (Object param : params) {
                sql.append(param);
            }
        }
        result = DBUtil.execute(conn, sql.toString());
        return result;
    }

    @Override
    public boolean doUpdate(Feedback feedback) throws SQLException {
        boolean result = false;
        String sql = "update `tb_feedback` set `fbID` = ?, `userID` = ?, `fbState` = ?, `fbDate` = ?, `fbContent` = ? where `fbID` = ?";
        Object[] params = TransformUtil.beanTOArray(feedback);
        result = DBUtil.execute(conn, sql, params, params[0]);
        return result;
    }

    @Override
    public boolean doUpdate(Object... params) throws SQLException {
        boolean result = false;
        StringBuffer sql = new StringBuffer("update `tb_feedback` ");
        if (null != params && params.length > 0) {
            for (Object param : params) {
                sql.append(param);
            }
        }
        result = DBUtil.execute(conn, sql.toString());
        return result;
    }

    @Override
    public Feedback getFeedback(long ID) throws SQLException {
        Feedback result = null;
        String sql = "select * `from tb_feedback` where `fbID` = ?";
        List<Object[]> results = DBUtil.query(conn, sql, ID);
        if (results.size() > 0) {
            result = (Feedback) TransformUtil.arrayToBean(results.get(0),
                    Feedback.class);
        }
        return result;
    }

    @Override
    public List<Feedback> getFeedback(Object... params) throws SQLException {
        List<Feedback> result = null;
        StringBuffer sql = new StringBuffer("select * from `tb_feedback` ");
        if (null != params && params.length > 0) {
            for (Object param : params) {
                sql.append(param);
            }
        }
        List<Object[]> all = DBUtil.query(conn, sql.toString());
        if (all.size() > 0) {
            result = new ArrayList<Feedback>();
            for (Object[] feedbacks : all) {
                Feedback feedback = new Feedback();
                feedback = (Feedback) TransformUtil.arrayToBean(feedbacks,
                        Feedback.class);
                result.add(feedback);
            }
        }
        return result;
    }

}
